package com.asa.ui.A;

import java.awt.EventQueue;
import java.util.Arrays;

import com.asa.ui.A.AlgoFrame;
import com.asa.ui.frame.AlgoVisHelper;


public class AlgoVisualizer {
	private AlgoFrame frame;
	private int[] money;
	public AlgoVisualizer(final int screenWidth,final int screenHeight,int Number){
		money=new int[Number];
		//初始化数据
		for(int i=0;i<Number;i++){
			money[i]=100;
		}
		//初始化视图
//		EventQueue.invokeLater(()->{
//			frame=new AlgoFrame("随机分钱开始问题模拟",screenWidth,screenHeight);
//			new Thread(()->run()).start();
//		});
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoFrame("随机分钱开始问题模拟",screenWidth,screenHeight);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
				
			}
		});
		
		
	}
	//播放动画
	public void runmiage(){
		while(true){
			//排序这样就无法确定那个人的财富量最大
			Arrays.sort(money);
			//绘制数据
			frame.render(money);
			//线程休眠20ms
			AlgoVisHelper.pause(40);
			//最外层循环意味着50倍的速度
			for(int k=0;k<50;k++){
			for(int i=0;i<money.length;i++){
				//if(money[i]>0){
					int j=(int)(Math.random()*money.length);
					money[i]--;
					money[j]++;
				//}
			}
			}
		}
	}

}